package com.bosssoft.setp.handler;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.AttachmentProxy;
import java.io.InputStream;

/**
 * 阿里云文件传输类
 *
 * @author clt
 */
@Service
public class AliOSSProxy implements AttachmentProxy {
    @Value("${aliyun.oss.end-point}")
    private String endpoint;

    @Value("${aliyun.oss.access-key-id}")
    private String accessKeyId;

    @Value("${aliyun.oss.access-key-secret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;

    /**
     * 上传文件
     * @param inputStream
     * @param path
     * @return
     */
    @Override
    public String upLoad(InputStream inputStream, String path) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        try {
            PutObjectResult result = ossClient.putObject(bucketName, path.substring(1), inputStream);
            if (result != null) {
                return path;
            }
        } finally {
            ossClient.shutdown();
        }
        return null;
    }

    /**
     * 获取文件域名公共部分
     * @return
     */
    @Override
    public String fileDomain() {
        return "https://scientificexperimenttrainingplatform.oss-cn-beijing.aliyuncs.com";
    }

    /**
     * 是否是本地存储
     * @return
     */
    @Override
    public boolean isLocalSave() {
        return false;
    }
}